#include <conio.h>
#include "FONT.H"
#include "INPUT.H"

#define FPS 30
#define DELTA 1000/FPS

int main()
{
	int tInput;	
	char text[45];
	int i, x=0, y=0, row = 0;	
	int index = 0;
	Fonts hollywood;
	boolean valid;
	valid = loadFonts("FONT.FNT", &hollywood);
	setMode(VGA_MODE);
	while(TRUE)
	{
		if(kbhit())
		{
			tInput = getKey();
			if(tInput == 27)
			{
				break;
			}
			if(valid)
			{
				if(index<45){
					if(tInput >= 32 && tInput < 128){
						text[index++] = tInput;
					}else if(tInput == 8){
						index--;
					}
					else if(tInput == 9 || tInput == 13 || tInput == 32){
						text[index++] = tInput	;
					}
				}				
			}
		}
		setBackground(LIGHT_BLUE);
		if(valid)
		{
			x=0;
			y=0;
			row = 0;
			for(i=0;i<index&&row<5;i++)
			{				
				if(text[i] > 32 && text[i] <= 127)
				{
					if(x+hollywood.defaultSymbol.width > SCREEN_WIDTH)
					{
						x = 0;
						y += (hollywood.defaultSymbol.height+2);
						row++;
					}
					if(row<5){
						drawFont(hollywood, text[i], x,y,BROWN);
					}
					x += (hollywood.defaultSymbol.width + 2);
				}				
				else if(text[i] == 9)
				{
					x += (hollywood.defaultSymbol.width + 2);
					x += (hollywood.defaultSymbol.width + 2);
				}
				else if(text[i] == 13)
				{
					x = 0;
					y += (hollywood.defaultSymbol.height+2);
					row++;
				}
				else if(text[i] == 32)
				{
					x += (hollywood.defaultSymbol.width + 2);
				}
			}
		}
		redraw();
		delay(DELTA);
	}
	freeFonts(&hollywood);
	setMode(TEXT_MODE);
	return EXIT_SUCCESS;
}
